package net.suncaper.psychological.mapper.impl;

import net.suncaper.psychological.mapper.UserMapper;
import net.suncaper.psychological.common.utils.MyDbUtil;
import net.suncaper.psychological.model.domain.User;

import java.util.List;

/**
 * 用户数据访问接口实现类
 */
public class UserMapperImpl implements UserMapper {

    @Override
    public List<User> getUserList() {
        // 默认只查询活跃用户
        String sql = "SELECT id, username, password, name, phone, gender, role, CAST(extra_info AS CHAR) as extra_info, is_active, create_time FROM user WHERE is_active = true";
        List<User> list = MyDbUtil.executeQuery(User.class, sql);
        System.out.println("数据库查到的活跃用户数量: " + list.size());
        for (User u : list) {
            System.out.println(u);
        }
        return list;
    }

    @Override
    public List<User> getAllUsers() {
        // 查询所有用户（包括已删除）
        String sql = "SELECT id, username, password, name, phone, gender, role, CAST(extra_info AS CHAR) as extra_info, is_active, create_time FROM user";
        List<User> list = MyDbUtil.executeQuery(User.class, sql);
        System.out.println("数据库查到的所有用户数量: " + list.size());
        return list;
    }

    @Override
    public User getUserById(Long id) {
        String sql = "SELECT id, username, password, name, phone, gender, role, CAST(extra_info AS CHAR) as extra_info, is_active, create_time FROM user WHERE id = ?";
        List<User> list = MyDbUtil.executeQuery(User.class, sql, id);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public User getUserByUsername(String username) {
        String sql = "SELECT id, username, password, name, phone, gender, role, CAST(extra_info AS CHAR) as extra_info, is_active, create_time FROM user WHERE username = ?";
        List<User> list = MyDbUtil.executeQuery(User.class, sql, username);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public User getUserByStudentId(String studentId) {
        // 直接用username字段匹配学号
        String sql = "SELECT id, username, password, name, phone, gender, role, CAST(extra_info AS CHAR) as extra_info, is_active, create_time FROM user WHERE username = ? LIMIT 1";
        List<User> list = MyDbUtil.executeQuery(User.class, sql, studentId);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public int insertUser(User user) {
        String sql = "INSERT INTO user(username, password, name, phone, gender, role, extra_info, is_active, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())";
        return MyDbUtil.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getName(), user.getPhone(), user.getGender(), user.getRole(), user.getExtra_info(), user.getIs_active());
    }

    @Override
    public int updateUser(User user) {
        String sql = "UPDATE user SET username=?, name=?, phone=?, gender=?, role=?, is_active=?, extra_info=? WHERE id=?";
        return MyDbUtil.executeUpdate(sql, user.getUsername(), user.getName(), user.getPhone(), user.getGender(), user.getRole(), user.getIs_active(), user.getExtra_info(), user.getId());
    }

    @Override
    public int deleteUser(Long id) {
        try {
            // 软删除：将 is_active 设置为 false
            String sql = "UPDATE user SET is_active = false WHERE id = ?";
            int result = MyDbUtil.executeUpdate(sql, id);
            System.out.println("软删除用户ID " + id + " 结果: " + result);
            return result;
        } catch (Exception e) {
            System.err.println("软删除用户时发生异常: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }
}
